# *-* coding:utf8 *-*
import json

import frida
import sys
from pathlib import Path
import time


def analysis_method_name(text):
    text = text.split('(')
    text = text[0]
    start = text.rfind('.')
    return text[start + 1:]


def on_message(message, data):
    if message['type'] == 'send':
        text = message['payload']
        print(text)
        # data = json.loads(text)['data']
        # method_list = json.loads(data)
        #
        # test = []
        # prev = ""
        # temp = ""
        # for item in method_list:
        #     current = analysis_method_name(item)
        #     if current in prev:
        #         index = int(prev.split('_')[1]) + 1
        #         temp = current + '_' + str(index)
        #         test.append(temp)
        #     else:
        #         temp = current + '_0'
        #         test.append(temp)
        #     prev = temp
        #
        # print(test)
    else:
        print(message)


class FridaTools:

    def __init__(self, script_path, is_usb=True, host=''):
        self.script_path = script_path
        self.load_script()
        self.devices(is_usb, host)
        self.script = None
        self.script_buffer = None
        self.device = None

    def __init__(self):
        self.script_path = ''

    def set_script_path(self, script_path):
        self.script_path = script_path

    def devices(self, is_usb, host=''):
        if is_usb:
            try:
                self.device = frida.get_usb_device()
            except Exception as e:
                self.device = frida.get_remote_device()
        else:
            if host:
                manager = frida.get_device_manager()
                self.device = manager.add_remote_device(host)
            else:
                self.device = frida.get_local_device()

    def attach_app(self, process, callback=on_message):
        try:
            self.session = self.device.attach(process)
            self.script = self.session.create_script(self.script_buffer)
            self.script.on('message', callback)
            self.script.load()
        except Exception as e:
            print(e)

    def swap_app(self, process, callback=on_message):
        try:
            pid = self.device.spawn(process)  # com.hx.hxsocket  'com.tencent.mobileqq'
            session = self.device.attach(pid)
            self.script = session.create_script(self.script_buffer)
            self.script.on('message', callback)
            self.script.load()
            self.device.resume(pid)
            time.sleep(2)
        except Exception as e:
            print(e)

    def unload(self):
        self.script.unload()

    def load(self, callback=on_message):
        self.script = self.session.create_script(self.script_buffer)
        self.script.on('message', callback)
        self.script.load()

    def detach(self):
        self.session.detach()

    def get_exports(self):
        return self.script.exports

    def load_script(self):
        with open(Path(__file__).resolve().parent.joinpath(self.script_path), encoding="utf-8") as f:
            self.script_buffer = f.read()


if __name__ == '__main__':

    frida_tools = FridaTools('script/frida/hook_script.js')
    frida_tools.attach_app('com.hx.hxsocket')




